/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou Ashe Dev, Ltd. All Right Reserved.
 * @address:     https://yiyuery.github.io/NoteBooks/
 * @date:        2020/2/23 11:40 上午
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package com.yi.yier.boot.autoconfigure.codegenerator.gen.model.impl;

import com.yi.yier.boot.autoconfigure.codegenerator.common.GenCodeParam;
import com.yi.yier.boot.autoconfigure.codegenerator.common.PropertyConstant;
import com.yi.yier.boot.autoconfigure.codegenerator.common.TemplateItem;
import com.yi.yier.boot.autoconfigure.codegenerator.gen.AbstractCodeGenerator;
import com.yi.yier.boot.autoconfigure.codegenerator.constants.CodeGenConstants;
import com.yi.yier.boot.autoconfigure.codegenerator.common.PropertiesLoader;
import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.VelocityContext;

import java.io.File;
import java.util.HashMap;

/**
 * <p>
 *
 * </p>
 *
 * @author Ashe
 * @version V1.0.0
 * @date 2020/2/23 11:40 上午
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人}  2020/2/23
 * @modify reason: {方法名}:{原因}
 * ...
 */
public class DefaultServiceGenerator extends AbstractCodeGenerator {

    @Override
    public void genCode(GenCodeParam genCodeParam) {
        String domainModelNameUpper = StringUtils.isEmpty(genCodeParam.getModelName()) ?
                tableNameConvertUpperCamel(genCodeParam.getTableName()) : genCodeParam.getModelName();
        try {
            String exportPath = PropertiesLoader.getProperty(PropertyConstant.YIER_GEN_PROP_EXPORT_PATH)
                    + PropertiesLoader.getProperty(PropertyConstant.YIER_GEN_PROP_PACKAGE_SERVICE).replaceAll("\\.", File.separator)
                    .replace("{sign}",genCodeParam.getSign())+File.separator;
            File controllerFile = initExportFile(exportPath
                    + domainModelNameUpper + "Controller.java");

            // 创建 Service 接口
            File serviceFile = initExportFile(exportPath
                    + domainModelNameUpper + "Service.java");

            // 创建 Service 接口的实现类
            File serviceImplFile = initExportFile(exportPath + "/impl/"
                    + domainModelNameUpper + "ServiceImpl.java");

            switch (CodeGenConstants.TemplateKey.vOf(PropertiesLoader.getProperty(PropertyConstant.YIER_GEN_PROP_TEMPLATE_TYPE))) {
                case VELOCITY:
                    genCodeWithVelocityTemplate(
                            TemplateItem.builder()
                                    .templateName("service.vm")
                                    .targetPath(serviceFile.getAbsolutePath())
                                    .className(domainModelNameUpper + "Service.java")
                                    .contextMap(getTemplateContext(genCodeParam, domainModelNameUpper, VelocityContext.class))
                                    .build(),
                            TemplateItem.builder()
                                    .templateName("serviceImpl.vm")
                                    .targetPath(serviceImplFile.getAbsolutePath())
                                    .className(domainModelNameUpper + "ServiceImpl.java")
                                    .contextMap(getTemplateContext(genCodeParam, domainModelNameUpper, VelocityContext.class))
                                    .build());
                    break;
                case FREEMARKER:
                    genCodeWithFreemarkerTemplate(TemplateItem.builder()
                                    .templateName("service.ftl")
                                    .targetPath(serviceFile.getAbsolutePath())
                                    .className(domainModelNameUpper + "Service.java")
                                    .contextMap(getTemplateContext(genCodeParam, domainModelNameUpper, HashMap.class))
                                    .build(),
                            TemplateItem.builder()
                                    .templateName("serviceImpl.ftl")
                                    .targetPath(serviceImplFile.getAbsolutePath())
                                    .className(domainModelNameUpper + "ServiceImpl.java")
                                    .contextMap(getTemplateContext(genCodeParam, domainModelNameUpper, HashMap.class))
                                    .build());
                default:
                    break;
            }
        } catch (Exception e) {
            throw new RuntimeException("Service 生成失败!", e);
        }
    }
}

